#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include<QQmlContext>
#include<QtDebug>
#include<QQmlComponent>
#include<QQmlProperty>
#include<QQuickItem>
#include"message.h"
#include"changeqmlcolor.h"
int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);
    QQmlApplicationEngine engine;
    qmlRegisterType<Message>("an.qt.Message", 1, 0, "Message"); //注册C++类
    Message msg;  //C++类
    engine.rootContext()->setContextProperty("msg2",&msg);  //注册C++类为QML属性
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

        QObject *object=engine.rootObjects().at(0);
        QObject * quitButton =object->findChild<QObject*>("quitButton");
        if(quitButton)
        {
            new ChangeQmlColor(quitButton);
            qDebug()<<"连接了QML退出 按钮到Cpp";
            QObject::connect(quitButton, SIGNAL(clicked()), &app, SLOT(quit()));
        }

        QVariant returnedValue;
        QVariant msg1="Hello from C++";
        //C++ 调用QML 方法
        QMetaObject::invokeMethod(object,"myQmlFunction",Q_RETURN_ARG(QVariant,returnedValue),Q_ARG(QVariant,msg1));
        qDebug()<<"QML Function returned:"<<returnedValue.toString();

   //C++ 调用QML 对象成员
        qDebug()<<"Property value:"<<QQmlProperty::read(object,"someValue").toInt();
        QQmlProperty::write(object,"someValue",5000);
        qDebug()<<"Property value 2:"<<object->property("someValue").toInt();
        object->setProperty("someValue",100);
        qDebug()<<"Property value 3:"<<object->property("someValue").toInt();
        qDebug()<<"Property width value:"<<object->property("width").toInt();
    QObject::connect(object,SIGNAL(qmlSignal(QString )),&msg,SLOT(qmlToCppSlot(QString )));   //连接QML信号到C++槽
    return app.exec();
}
